const Router = require('koa-router')
//

const {
  create,
  update,
  get,
  list,
  del,
  login,
  updatePwd,
  unlock,
  getByID,
  logout,
} = require('../../../controller/v1/admin/user')

const {
  createValidator,
  updateValidator,
  delValidator,
  listValidator,
  loginValidator,
  updatePwdValidator,
  unlockValidator,
} = require('../../../middleware/validator/v1/user')

// validateToken, validateAdmin 有先后顺序 先校验token再校验管理员
const {
  validateToken,
  validateAdmin,
} = require('../../../middleware/auth/token')
// 密码解密
const { passwdDecryptSm4 } = require('../../../middleware/decrypt/passwd')

const { idParseInt } = require('../../../middleware/convert/parseInt')

const { reqLog, timeLog } = require('../../../middleware/log/log')
const PREFIX = '/v1/admin/user'

const router = new Router({ prefix: PREFIX })

router.get('/get', validateToken, get)
//
router.get('/getById', validateToken, idParseInt, getByID)

router.put(
  '/update',
  validateToken,
  updateValidator,
  passwdDecryptSm4,
  reqLog,
  update
)

router.post('/login', timeLog, loginValidator, passwdDecryptSm4, reqLog, login)

router.post(
  '/unlock',
  validateToken,
  validateAdmin,
  unlockValidator,
  reqLog,
  unlock
)

router.put(
  '/updatePwd',
  validateToken,
  updatePwdValidator,
  passwdDecryptSm4,
  reqLog,
  updatePwd
)

// 后端管理员权限
router.post(
  '/create',
  // validateToken,
  // validateAdmin,
  createValidator,
  passwdDecryptSm4,
  reqLog,
  create
)

// 后端管理员权限
router.del('/del', validateToken, validateAdmin, delValidator, reqLog, del)

// 后端管理员权限
router.get('/list', validateToken, validateAdmin, listValidator, list)

// 后端管理员权限
router.get('/logout', validateToken, reqLog, logout)

module.exports = router
